package com.mls.fivedistrict.controller.backend;

import com.github.pagehelper.PageInfo;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.controller.BaseController;
import com.mls.fivedistrict.exception.CustomerException;
import com.mls.fivedistrict.pojo.UserAuth;
import com.mls.fivedistrict.pojo.UserAuthAndUser;
import com.mls.fivedistrict.server.UserAuthService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Optional;

/**
 * Created by lixixi on 2018/4/2.
 */
@RestController("backendUserAuthController")
@RequestMapping("/api/manager")
public class UserAuthController extends BaseController {
    @Resource
    private UserAuthService userAuthService;

    @GetMapping("/v1/userAuths")
    public ServiceResponse getUserAuthList(HttpServletRequest httpServletRequest) {
        PageInfo pageInfo = userAuthService.getUserAuthList(getPageFilter());
        return ServiceResponse.createSuccessResponse(pageInfo);
    }

    @GetMapping("/v1/userAuth/{userAuthId}")
    public ServiceResponse getUserAuth(@PathVariable("userAuthId") Long userAuthId) {
        UserAuth userAuth = userAuthService.getUserAuth(userAuthId);
        if (userAuth != null) {
            return ServiceResponse.createSuccessResponse(userAuth);
        }
        return ServiceResponse.createErrorResponse();
    }
    @PutMapping("/v1/userAuth/editUserAuthAndUser/{userAuthId}")
    public ServiceResponse editUserAuthCranoPhoneRealName(@PathVariable("userAuthId") Long userAuthId, @RequestBody UserAuthAndUser userAuthAndUser){
        userAuthAndUser.setId(userAuthId);
        Optional.ofNullable(userAuthAndUser.getId()).orElseThrow(()->new CustomerException("用户认证ID不能为空"));
        Optional.ofNullable(userAuthAndUser.getCradNo()).orElseThrow(()->new CustomerException("用户的证件号码不能为空"));
        Optional.ofNullable(userAuthAndUser.getRealName()).orElseThrow(()->new CustomerException("用户的真实姓名不能为空"));
        return userAuthService.editUserAuthAndUser(userAuthAndUser,getToken());
    }

    @PutMapping("/v1/userAuth/{userAuthId}")
    public ServiceResponse editUserAuth(@PathVariable("userAuthId") Long userAuthId, @RequestBody UserAuth userAuth) {
        userAuth.setId(userAuthId);
        userAuth.setUpdateDate(new Date());
        Optional.ofNullable(userAuth.getStatus()).orElseThrow(() -> new CustomerException("状态不能为空"));
        int row = userAuthService.editUserAuth(userAuth,getToken());
        if (row > 0) {
            return ServiceResponse.createSuccessResponse();
        }
        if (row == -1) {
            return ServiceResponse.createErrorResponse("请勿修改相同状态");
        }
        return ServiceResponse.createErrorResponse();
    }

    /**
     * 获取用户的搜索结果
     * @return
     */
    @RequestMapping("/v1/userAuth/search")
    public ServiceResponse search(){
        ServiceResponse response=userAuthService.search(getPageFilter());
        return response;
    }

}
